package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 描述:
 * @date: 2020/8/4 13:13
 * @author: summer
 * version: 1.0
 */
@RequestMapping("/department")
@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 功能描述: 添加部门数据
     * @param department
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/4
     */
    //@RequestMapping(value = "save", method = RequestMethod.PUT)
    @PutMapping
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败" +e.getMessage());
        }
    }

    /**
     * 功能描述: 修改部门 数据
     * @param department
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/4
     */
    //@RequestMapping(value = "update", method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Department department){
        System.out.println(department);
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败" + e.getMessage());
        }
    }

    /**
     * 功能描述: 根据id删除部门数据
     * @param id
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/4
     */
    //@RequestMapping(value = "delete", method = RequestMethod.DELETE)
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败" + e.getMessage());
        }
    }

    /**
     * 功能描述: 根据id查找部门对象
     * @param id
     * @return: cn.itsource.pethome.org.domain.Department
     * @Date: 2020/8/4
     */
    //@RequestMapping(value = "loadById", method = RequestMethod.GET)
    @GetMapping("{id}")
    public Department loadById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 功能描述: 查找所有部门
     * @param
     * @return: java.util.List<cn.itsource.pethome.org.domain.Department>
     * @Date: 2020/8/4
     */
    //@RequestMapping(value = "loadAll", method = RequestMethod.PATCH)
    @PatchMapping
    public PageList<Department> loadAll(@RequestBody DepartmentQuery departmentQuery){
        //departmentQuery.toString();
        //System.out.println(departmentQuery);
        return departmentService.loadAll(departmentQuery);
    }

    /**
     * 功能描述: 根据id，批量删除部门列表
     * @param ids
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/8
     */
    @PatchMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
//        for (Long id : ids) {
//            System.out.println(id);
//        }
        try {
            departmentService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败" + e.getMessage());
        }
    }

    /**
     * 功能描述: 查询所有部门
     * @return: cn.itsource.pethome.basic.util.PageList<cn.itsource.pethome.org.domain.Department>
     * @Date: 2020/8/8
     */
    @PatchMapping("/findDepartment")
    public List<Department> findDepartment(){
        return departmentService.findDepartment();
    }
}
